package com.busdepot.webbapp.domain;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.busdepot.webapp.domain.Customer;
import com.busdepot.webapp.domain.CustomerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/spring-service.xml" })
public class CustomerServiceTestIntegration {

	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private CustomerService customerService;

	Long time = System.currentTimeMillis();
	
	@Test
	public void saveNewCustomer() {
		Customer customer = new Customer();
		String username = "DRumsfield";
		customer.setUsername(username);
		customer.setPassword("tough");
		customer.setFirstName("Donald");
		customer.setLastName("Rumsfield");
		customer.setAddress1("100 Main St");
		customer.setAddress2("none");
		customer.setCity("Pottstown");
		customer.setState("PA");
		customer.setZip(65498);
		customer.setCountry("USA"); // database can only hold 3 characters
		customer.setAreaCode(123);
		customer.setExchange(456);
		customer.setNumber(7896);
		customer.setEmail("rummy@gmail.com");
		customer.setPersonalMemo("Freedom");
		customer.setShippingPreference("Red"); // database can only hold 10
												// characters
		customer.setBackorderedPreference("Separate"); // database can only hold
														// 10 characters
		customer.setCommentPreference("Don't leave crap on my porch");
		customer.setVehicleModel("1968 Vanawagon");
		customer.setShippingFirstName("Don");
		customer.setShippingLastName("Rum");
		customer.setShippingEmail("don@gmail.com");
		customer.setShippingAddress1("234 Hill Road");
		customer.setShippingAddress2("Apt 4");
		customer.setShippingCity("Royersford");
		customer.setShippingState("TX");
		customer.setShippingZip(65498);
		customer.setShippingCountry("USA");
		customer.setShippingAreaCode(987);
		customer.setShippingExchange(984);
		customer.setShippingNumber(3467);

		customerService.save(customer);
		System.out.println("customer saved..." + customer);
	}
	
	@Test
	public void findCustomerByUsername() {
		String username = "DRumsfield";
		List<Customer> customerList = customerService.getCustomerByUsername(username);
		Assert.assertTrue("customerList is empty", !customerList.isEmpty());
		System.out.println("customers found by username " + username);
	}
	
	@Test 
	public void updateCustomer(){
		Long time = System.currentTimeMillis();
		String username = "DRumsfield";
		List<Customer> customerList = customerService.getCustomerByUsername(username);
		Customer customer = customerList.get(0);
		customer.setAddress1("Dunkin Donuts" + time);
		customerService.save(customer);
		System.out.println(username + "'s address was updated to Dunkin Donuts " + time);
	}

}
